<template>
  <div class="distribute-lingyong-kj-form">
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <div style="margin:10px">分配明细</div>
      <el-table
        :loading="false"
        :table-height="300"
        :data="formItem.lingyong"
        :disable-pageable="true"
        class="fixed-table-table"
      >
        <el-table-column
          label="序号"
          type="index"
          width="50"
        />
        <el-table-column v-for="column in insideColumns2" :key="column.key" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>

        <el-table-column label="位置">
          <template slot-scope="scope">
            <treeselect
              v-model="scope.row.lingyongLocation"
              :multiple="false"
              :options="availableLocations"
              :load-options="loadOptions"
              placeholder="请选择位置"
            />
          </template>
        </el-table-column>

        <el-table-column label="使用人">
          <template slot-scope="scope">
            <el-select v-model="scope.row.managementUserCode" size="mini" clearable placeholder="请选择使用人">
              <el-option v-for="item in users" :key="item.username" :label="item.name" :value="item.username" />
            </el-select>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right; margin-top:10px">
        <el-form-item>
          <el-button size="mini" @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="commit">提 交</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<script>
import { distributeCreate as create, distributeDetail as detail } from '@neu/dcam-ui/api/lingyongKJ'
// import PageableTable from '@neu/biz-core/components/PageableTable'
import { list as userList } from '@neu/auth-ui/api/user'
import { tree as locationTree } from '@neu/dcam-ui/api/location'
import treeselect from '@neu/biz-core/components/TreeSelect'
const formItem = {
  title: '',
  reason: '',
  remark: '',
  comprehensive: true,
  recipientRequires: [],
  createUser: '',
  orgCode: '',
  orgName: '',
  lingyong: []
}

export default {
  components: {
    // PageableTable,
    treeselect
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    },
    categories: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      availableLocations: null,
      users: [],
      formItem: { ...formItem },
      insideColumns2: [
        { title: '资产名称', width: 100, key: 'name' },
        { title: '资产编号', width: 120, key: 'code' },
        { title: '大分类', width: 100, key: 'bigTypeName' },
        { title: '中分类', width: 100, key: 'middleTypeName' },
        { title: '小分类', width: 100, key: 'smallTypeName' },
        { title: '规格型号', width: 100, key: 'specifications' },
        { title: '序列号', width: 100, key: 'serialnumber' }

      ],
      rules: {},
      selectTypeCode: '',
      selectSpecifications: '',
      ddd: ''
    }
  },
  created() {
    this.fetch()
    // 位置树
    // locationTree('kj').then(response => {
    //   this.availableLocations = response.data
    // })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }

          userList({ 's_orgCodes': this.formItem.orgCode }).then(res => {
            this.users = res.data
          })
        })
      }
    },
    commit() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          // 调拨资产不能为空
          if (this.formItem.lingyong.length === 0) {
            return this.$message({
              showClose: true,
              message: '分发资产为空，请填写资产明细后在提交!',
              type: 'error'
            })
          }

          const data = []
          const locations = this.flattenTreeDataClosure(this.availableLocations)
          this.formItem.lingyong.forEach(l => {
            const item = { ...l }
            item.workOrderNum = this.workOrderNum
            item.taskId = this.taskId
            const one = this.users.find(s => s.username === item.managementUserCode)
            item.managementUserName = one ? one.name : ''

            // 位置
            const location = locations.find(s => s.code === item.lingyongLocation)
            item.lingyongLocationName = location ? location.title : ''
            data.push(item)
          })
          // ge.x 2022-04-08 增加锁屏功能
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          create(data).then(res => {
            if (res.status === 'success') {
              loading.close()
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          }).catch(() => {
            loading.close()
            this.$emit('cancelNewOrderForm', 'refresh')
          })
        }
      })
    },
    btnCancel() {
      this.$emit('cancelNewOrderForm', 'refresh')
    },
    // 扁平化树形结构
    flattenTreeDataClosure(data) {
      const treeData = data
      const flattenData = []
      function flattenTree(data, parentKey) {
        data.forEach(ele => {
          const { title, code, children } = ele
          flattenData.push({ title, code, parentKey })
          if (children) {
            flattenTree(children, code)
          }
        })
      }
      flattenTree(treeData, null)
      return flattenData
    },
    loadOptions({ action }) {
      if (action === 'LOAD_ROOT_OPTIONS') {
        // return tree().then(response => {
        //   this.availableOganizations = [response.data]
        // })
        return locationTree('kj').then(response => {
          this.availableLocations = response.data
        })
      }
    }
  }
}
</script>

<style lang="scss">
  .process-form {
    .el-card {
      overflow: visible;
    }
    .el-card__body {
      overflow: visible;
    }
  }
  .distribute-lingyong-kj-form {
    .el-table{
      overflow: visible;
    }
    .el-table .cell{
      overflow: visible;
    }
    .el-table__body-wrapper{
      overflow: visible;
    }
  }
</style>
